//
//  VideoCell.m
//  News
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "VideoCell.h"
#import "VideoModel.h"
#import "UIImageView+WebCache.h"
#import "AFNetworkReachabilityManager.h"
#import "MBProgressHUD+MJ.h"
#import "DownloadListController.h"
#import "VideoDownLoadManager.h"

@interface VideoCell ()

@property (weak, nonatomic) IBOutlet UIImageView *videoImage;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLengthLabel;
@property (weak, nonatomic) IBOutlet UILabel *playCountLabel;
@property (weak, nonatomic) IBOutlet UIView *bottomView;


@end

@implementation VideoCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
    
}




#pragma mark - 初始化cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cell_video = @"cell_video";
    VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_video];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"VideoCell" owner:nil options:nil] lastObject];
        cell.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
        cell.bottomView.layer.cornerRadius = 20;
        cell.bottomView.layer.masksToBounds = YES;
        
        NSLog(@"%s", __func__);
    }
    return cell;
}


#pragma mark - 重写model属性的set方法, 给控件赋值
- (void)setModel:(VideoModel *)model
{
    _model = model;
    [self.videoImage sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    self.videoImage.contentMode = UIViewContentModeScaleAspectFill;
    self.videoImage.clipsToBounds = YES;
    
    self.playCountLabel.text = [NSString stringWithFormat:@"%@", model.playCount];
    self.playCountLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
    self.playCountLabel.font = [UIFont systemFontOfSize:14];
    
    self.timeLengthLabel.text = [self convertSecondToMinute:[model.length_video floatValue]];
    self.timeLengthLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
    self.timeLengthLabel.font = [UIFont systemFontOfSize:14];
    
    self.titleLabel.text = model.title;
    
    self.descLabel.text = model.description_video;
    self.descLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
    self.descLabel.font = [UIFont systemFontOfSize:14];
}

- (IBAction)downloadClick:(UIButton *)sender
{
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                
                
                // 判断是否下载过
                for (VideoModel *model in [VideoDownLoadManager sharedVideoDownLoadManager].downVideoArray) {
                    if ([model.mp4_url isEqualToString:self.model.mp4_url]) {
                        [MBProgressHUD showError:@"请勿重复下载"];
                        return ;
                    }
                }
               
                // 添加模型, 开始下载
                [[VideoDownLoadManager sharedVideoDownLoadManager] startAVideoWithVideoModel:self.model];
                [MBProgressHUD showSuccess:@"添加到下载列表"];
                
               
                [[DownloadListController sharedDownloadListController].tableView reloadData];
                
                break;
            }
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前使用流量是否继续下载?" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    // 判断是否下载过
                    for (VideoModel *model in [VideoDownLoadManager sharedVideoDownLoadManager].downVideoArray) {
                        if ([model.mp4_url isEqualToString:self.model.mp4_url]) {
                            [MBProgressHUD showError:@"请勿重复下载"];
                            return ;
                        }
                    }
                    
                    // 添加模型, 开始下载
                    [[VideoDownLoadManager sharedVideoDownLoadManager] startAVideoWithVideoModel:self.model];
                    [MBProgressHUD showSuccess:@"添加到下载列表"];
                    [[DownloadListController sharedDownloadListController].tableView reloadData];
                    
                    
                    
                }];
                UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:action1];
                [alert addAction:action2];
                if (self.delegate && [self.delegate respondsToSelector:@selector(videoCell:ShowAlertController:)]) {
                    [self.delegate videoCell:self ShowAlertController:alert];
                }
                
                break;
            }
                
            case AFNetworkReachabilityStatusNotReachable:
                [MBProgressHUD showError:@"网络无连接"];
                break;
            
                
            case AFNetworkReachabilityStatusUnknown:
            {
                [MBProgressHUD showError:@"网络异常"];
                break;
            }
        }
    }];
    
    [mgr startMonitoring];
    
}

// 将秒数转换为字符串
- (NSString *)convertSecondToMinute:(CGFloat)seconds
{
    int minute = seconds / 60;
    int second = (int)seconds % 60;
    
    NSString *timeLabel = [NSString stringWithFormat:@"%02d:%02d", minute, second];
    return timeLabel;
}

@end
